/**
 * index.js
 * 
 * @since 1.0.0.0 <2015-3-23> SoChishun <14507247@qq.com> Added.
 */

$(function () {
    // 垂直居中效果 2015-12-1 SoChishun Added.
    $(window).resize(function () {
        var $w = $(this);
        $('.login-panel').height($w.height());
        var $p = $('.login-form');
        $p.css({
            position: 'absolute',
            left: ($w.width() - $p.outerWidth()) / 2,
            top: ($w.height() - $p.outerHeight()) / 2 + $(document).scrollTop()
        });
    }).resize();
    // 点击刷新验证码
    $('.change-captcha').click(function () {
        var $img = $(this).find('img');
        $img.attr('src', $img.attr('src'));
        return false;
    })
    // 2014-9-27 by sutroon 新增验证码动态校验功能
    $('input[name="captcha"]').keyup(function () {
        var $input = $(this);
        var val = $input.val();
        if (val.length > 3) {
            var url = $(this).data('confirmUrl').replace('varkey', val);
            $.get(url, function (data) {
                var $em = $input.parent().find('em');
                if ($em[0]) {
                    $em.removeClass('error');
                    $em.removeClass('success');
                    $em.addClass(data ? 'success' : 'error').text(data ? '[正确]' : '[错误]');
                } else {
                    $input.after(data ? '<em class="success">[正确]</em>' : '<em class="error">[错误]</em>');
                }
            });
        } else {
            $input.parent().find('em').remove();
        }
    })
    $('.form-field-checkbox input').click(function () {
        var $chk = $(this);
        var str = '';
        if ($chk.prop('checked')) {
            Cookies.set($chk.val(), escape($('input.' + $chk.val()).val()))
        } else {
            Cookies.remove($chk.val());
        }
    })
    read_member();

// 表单验证 2015-3-23
    $('#frm-login').validate({
        errorElement: "em",
        rules: {
            login_name: {required: true, minlength: 3, maxlength: 16},
            password: {required: true, minlength: 1, maxlength: 32}
        }
    });
});
// 2015-5-5
function read_member() {
    var user = Cookies.get('tb_user');
    var pwd = Cookies.get('tb_pwd');
    if (user) {
        user = unescape(user);
        $('input.tb_user').val(user);
        $('.form-field-checkbox input[value="tb_user"]').prop('checked', true);
    }
    if (pwd) {
        pwd = unescape(pwd);
        $('input.tb_pwd').val(pwd);
        $('.form-field-checkbox input[value="tb_pwd"]').prop('checked', true);
    }
}